﻿using RBAC.LuFang.Domain;
using RBAC.LuFang.Domain.DTO;
using RBAC.LuFang.Repository.lnterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Collections.Specialized.BitVector32;

namespace RBAC.LuFang.Domin.Services
{
    public class LeaveapplicationService: ILeaveapplicationService
    {
        private readonly ILeaveapplicationRepository _leavRep;
        private readonly IUserRepository _userRep;
        private readonly IRoleRepository _roleRep;
        private readonly IUserRoleRepository _userroleRep;

        public LeaveapplicationService(ILeaveapplicationRepository leavRep, IUserRepository userRep, IRoleRepository roleRep,IUserRoleRepository userRoleRepository)
        {
            _leavRep = leavRep;
            _userRep = userRep;
            _roleRep = roleRep;
            _userroleRep = userRoleRepository;
        }
        /// <summary>
        /// 请假添加
        /// </summary>
        /// <param name="leaveapplication"></param>
        /// <returns></returns>
        public async Task<int> AddLeaveapplication(Leaveapplication leaveapplication)
        {
            try
            {
                leaveapplication.State = "待审核";
                leaveapplication.LeaveapplicationDate= DateTime.Now;
                return await _leavRep.GetAddAsync(leaveapplication);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 反填用户名
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public async Task<List<GetLeaveapplicationFanJWTDTO>> GetLeaveapplicationFanJWT(int uid)
        {
            try
            {
                var use = await _userRep.GetAllAsync();
                var res = (from u in use
                           where u.userInfoID == uid
                           select new GetLeaveapplicationFanJWTDTO
                           {
                               userName=u.userName,
                               sectionId=u.sectionId,
                               value=use.Where(x=>x.sectionId==u.sectionId && x.Isleader==true).Select(x=>new UserDTo11
                               {
                                   userName=x.userName
                               }).ToList()
                           }).ToList();
                return res;


            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 请假列表
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public async Task<LeavPage> GetLeaveapplication(int uid, int pageindex, int pagesize, string? kdate, string? sdate, string? state, string? leavname)
        {
            try
            {
                LeavPage page = new LeavPage();
                var res=await _userRep.GetByidAsync(uid);
                if (res.Isleader == true)
                {
                    var aaa = (await _leavRep.GetAllAsync()).Where(x => x.LeaveapplicationName == res.userName || x.sectionId == res.sectionId).ToList();
                    if (!string.IsNullOrEmpty(kdate))
                    {
                        aaa = aaa.Where(x => x.LeaveapplicationDate>=Convert.ToDateTime(kdate)).ToList();
                    }
                    if (!string.IsNullOrEmpty(sdate))
                    {
                        aaa = aaa.Where(x => x.LeaveapplicationDate < Convert.ToDateTime(sdate).AddDays(1)).ToList();
                    }
                    switch (state)
                    {
                        case "待审核":
                            aaa = aaa.Where(x => x.State =="待审核").ToList();
                            break;
                        case "已审核":
                            aaa = aaa.Where(x => x.State == "已审核").ToList();
                            break;
                        default:
                            break;
                    }
                    if (!string.IsNullOrEmpty(leavname))
                    {
                        aaa = aaa.Where(x => x.LeaveapplicationName.Contains(leavname)).ToList();
                    }
                    page.sumcount = aaa.Count();
                    page.lastpage = (int)Math.Ceiling(page.sumcount * 1.0 / pagesize);
                    page.list = aaa.OrderByDescending(x => x.LeaveapplicationDate).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
                    return page;
                }
                else
                {
                    var bbb = (await _leavRep.GetAllAsync()).Where(x => x.LeaveapplicationName == res.userName).ToList();
                    if (!string.IsNullOrEmpty(kdate))
                    {
                        bbb = bbb.Where(x => x.LeaveapplicationDate >= Convert.ToDateTime(kdate)).ToList();
                    }
                    if (!string.IsNullOrEmpty(sdate))
                    {
                        bbb = bbb.Where(x => x.LeaveapplicationDate <= Convert.ToDateTime(sdate)).ToList();
                    }
                    switch (state)
                    {
                        case "待审核":
                            bbb = bbb.Where(x => x.State == "待审核").ToList();
                            break;
                        case "已审核":
                            bbb = bbb.Where(x => x.State == "已审核").ToList();
                            break;
                        default:
                            break;
                    }
                    if (!string.IsNullOrEmpty(leavname))
                    {
                        bbb = bbb.Where(x => x.LeaveapplicationName.Contains(leavname)).ToList();
                    }
                    page.sumcount = bbb.Count();
                    page.lastpage = (int)Math.Ceiling(page.sumcount * 1.0 / pagesize);
                    page.list = bbb.OrderByDescending(x => x.LeaveapplicationDate).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
                    return page;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除请假
        /// </summary>
        /// <param name="leavid"></param>
        /// <returns></returns>
        public async Task<int> DelLeaveapplication(int leavid)
        {
            try
            {
                var leavmodel = await _leavRep.GetByidAsync(leavid);
                return await _leavRep.Del(leavmodel);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="leavid"></param>
        /// <returns></returns>
        public async Task<Leaveapplication> FanLeaveapplication(int leavid)
        {
            try
            {
                return await _leavRep.GetByidAsync(leavid);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改审批
        /// </summary>
        /// <param name="leavid"></param>
        /// <param name="State"></param>
        /// <returns></returns>
        public async Task<int> UpdShenPiLeaveapplication(int leavid, string State)
        {
            try
            {
                var leavmodel = await _leavRep.GetByidAsync(leavid);
                if (State == "通过")
                {
                    leavmodel.State = "已审核通过";
                }
                if(State == "不通过")
                {
                    leavmodel.State = "审核未通过";
                }
                return await _leavRep.GetUpd(leavmodel);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 请假修改
        /// </summary>
        /// <param name="leavid"></param>
        /// <returns></returns>
        public async Task<int> Upd(Leaveapplication leaveapplication)
        {
            try
            {
                return await _leavRep.GetUpd(leaveapplication);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
